var NewProdCate = function(){
	
	var left_margin_step = 15;
	
	this.init = function(){
		$("#newAddProdCate").live(
				'click',
				function() {
					var pk_org = $("#vendorOrg").val();
					var pk_group = $("#vendorGroup").val();
					if((pk_org==null||pk_org=="")){
						showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
						return;
					}
					
					var myts = new Date();
					var formStr = $('#newProd_class_filter').serialize();
					jQuery.ajax( {
						type : "POST",
						async : false,
						url : ebvp_context + "/register/qryTopLevelMarBasCls?myts=" + myts +"&pk_org=" + pk_org + "&pk_group="+pk_group,
						dataType : "text",
						data : "&" + formStr,
						success : function(result) {
							if (result != null && result != "[]") {
								var arr = eval(result);
								$("#newProdcate_tree_list").empty();
								for ( var loop = 0; loop < arr.length; loop++) {
									var obj= arr[loop];
									var a = $("<a></a>").attr(
															"href", "#").addClass(
															"newProdcate_category_trigger close")
															.attr("margin", 0);
									var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
									var inputObj = $("<input/>")
															.val(obj.pk_marbasclass)
															.attr("type", "checkbox").attr("name", "pkProdcateCheckbox");
									var chooseAllLink = null;
									if(obj.isLeaf == "N"){
										inputObj.attr("disabled", "true").css("visibility","hidden");
										chooseAllLink = $("<a></a>").attr({"title":"全选该分类下所有下级分类"}).addClass("chooseAllLink").text("全选");
										a.removeClass("close");
									}
									var label = $("<label></label>")
															.text(obj.code + " " + obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
									
									var div = $("<div></div>")
															.addClass("prodcate_tree_category")
															.append(a).append(pk).append(inputObj).append(label).append(chooseAllLink);
									var existProdCl = $("#newProdCateDivs").find("[name='pkProdCl']");
									if(existProdCl != null && existProdCl.length !=0){
										for(var l=0; l<existProdCl.length; l++){
											if($(existProdCl[l]).val() == arr[loop].pk_marbasclass){
												break;
											}
										}
										if(l == existProdCl.length){
											$("#newProdcate_tree_list").append(div);
										}
									}else{
										$("#newProdcate_tree_list").append(div);
									}
								}
								$("#newProdcatePicker").find("#newProdPkChecked").find("input").remove();
							}else{
								$("#newProdcate_tree_list").empty();
							}
						}
					});
				});
		
		$("#newFilterProdClass").live(
				'click',
				function() {
					var pk_org = $("#vendorOrg").val();
					var pk_group = $("#vendorGroup").val();
					if((pk_org==null||pk_org=="")){
						showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
						return;
					}
					
					var myts = new Date();
					var formStr = $('#newProd_class_filter').serialize();
					jQuery.ajax( {
						type : "POST",
						async : false,
						url : ebvp_context + "/register/qryTopLevelMarBasCls?myts=" + myts +"&pk_org=" + pk_org + "&pk_group="+pk_group,
						dataType : "text",
						data : "&" + formStr,
						success : function(result) {
							if (result != null && result != "[]") {
								var arr = eval(result);
								$("#newProdcate_tree_list").empty();
								for ( var loop = 0; loop < arr.length; loop++) {
									var obj= arr[loop];
									var a = $("<a></a>").attr(
															"href", "#").addClass(
															"newProdcate_category_trigger close")
															.attr("margin", 0);
									var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
									var inputObj = $("<input/>")
															.val(obj.pk_marbasclass)
															.attr("type", "checkbox").attr("name", "pkProdcateCheckbox");
									var chooseAllLink = null;
									if(obj.isLeaf == "N"){
										inputObj.attr("disabled", "true").css("visibility","hidden");
										chooseAllLink = $("<a></a>").attr({"title":"全选该分类下所有下级分类"}).addClass("chooseAllLink").text("全选");
										a.removeClass("close");
									}
									var label = $("<label></label>")
															.text(obj.code + " " + obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
									
									var div = $("<div></div>")
															.addClass("prodcate_tree_category")
															.append(a).append(pk).append(inputObj).append(label).append(chooseAllLink);
									var existProdCl = $("#newProdCateDivs").find("[name='pkProdCl']");
									if(existProdCl != null && existProdCl.length !=0){
										for(var l=0; l<existProdCl.length; l++){
											if($(existProdCl[l]).val() == arr[loop].pk_marbasclass){
												break;
											}
										}
										if(l == existProdCl.length){
											$("#newProdcate_tree_list").append(div);
										}
									}else{
										$("#newProdcate_tree_list").append(div);
									}
								}
								$("#newProdcatePicker").find("#newProdPkChecked").find("input").remove();
							}else{
								$("#newProdcate_tree_list").empty();
							}
						}
					});
				});
		
		$("#newAddProdCate").UIDropDialog( {
			'triggerClass' : "idTreePicker",
			'content' : $("#newProdcatePicker")
		});

		$("#newProdcatePicked").live('click', function() {
			var items = $(this).parents(".ui-drop_dialog").find("#newProdPkChecked").find("input");
			
			if (items != null && items.length > 0) {
				/*
				if($("#newProdCateDivs").find(".new_prod_header").length==0){
					var newProdHeader = $("#newProdDescDemo").find(".new_prod_header").clone();
					$("#newProdCateDivs").append(newProdHeader);
				}*/
				
				for (var loop = 0; loop < items.length; loop++) {
					var newrow = $("#newProdDescDemo").find(".new_prodCate_div").clone();
					
					var prodcatename = $(items[loop]).attr("prodname");//产品分类code + " " + name
					var prodqa = $(items[loop]).attr("qaDesc");
					var pkProdCl = $(items[loop]).val();
					newrow.find(".new_prodCate_name").text(prodcatename);
					newrow.find(".new_prod_num").text("");
					newrow.find(".new_prod_qa").text(prodqa).attr("title", prodqa);
					newrow.find("[name='pkProdCl']").val(pkProdCl);
					prodcatename = prodcatename.substring(prodcatename.indexOf(" ") + 1);
					newrow.find("[name='prodClName']").val(prodcatename);
					newrow.find("[name='qaDescription']").val(prodqa);
					$("#newProdCateDivs").append(newrow);
				}
			}
			
			$(this).parents(".ui-drop_dialog").slideUp();
			return false;
		});

		$("#newProdcateCancel").live('click', function() {
			$(this).parents(".ui-drop_dialog").slideUp();
			return false;
		});

		$(".newProdcate_category_trigger").live(
				'click',
				function() {
					
					var category_triggerObj=$(this);
					if(category_triggerObj.hasClass("close")){
						category_triggerObj.parent(".prodcate_tree_category").next(".children").slideUp(function(){
							category_triggerObj.removeClass("close");
						});
						return false;
					}
					
					$(this).addClass("close");
					
					var pk_org = $("#vendorOrg").val();
					var pk_group = $("#vendorGroup").val();
					if( (pk_org==null||pk_org=="")  ){
						showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
						return;
					}
					var pk_parent = category_triggerObj.next().val();
					
					var myts = new Date();
					var formStr = $('#prod_class_filter').serialize();
					jQuery.ajax( {
						type : "POST",
						async : false,
						url : ebvp_context
								+ "/register/qryNextLevelMarBasClsByPkParent?pk_parent="+pk_parent+"&myts=" + myts+"&pk_org="+pk_org+"&pk_group="+pk_group,
						dataType : "text",
						data : "&" + formStr,
						success : function(result) {
						
							if(result == "[]"){
								return false;
							}
							var orgArr = eval(result);
							
							var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
							
							var childrenDiv = $("<div></div>").addClass("children");
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];

								var a = $("<a></a>").attr("href", "#").addClass(
										"newProdcate_category_trigger close").attr("style","margin-left:"+current_margin_left_size+"px").attr("margin", current_margin_left_size);
								var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
								var inputObj = $("<input/>").val(obj.pk_marbasclass).attr(
										"type", "checkbox").attr("name", "pkProdcateCheckbox");
								//չ��
								$("#newProdPkChecked").find("input").each(function(){
									if(obj.pk_marbasclass == $(this).val()){
										inputObj.attr("checked", "checked");
									}
								});
								var chooseAllLink = null;
								if(obj.isLeaf == "N"){
									inputObj.attr("disabled", "true").css("visibility","hidden");
									chooseAllLink = $("<a></a>").attr({"title":"全选该分类下所有下级分类"}).addClass("chooseAllLink").text("全选");
									a.removeClass("close");
								}
								var label = $("<label></label>").text(obj.code + " " + obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
								
								var div = $("<div></div>")
									.addClass("prodcate_tree_category").append(a).append(pk).append(inputObj).append(label).append(chooseAllLink);
													
								var existProdCl = $("#newProdCateDivs").find("[name='pkProdCl']");
								if(existProdCl != null && existProdCl.length !=0){
									for(var l=0; l<existProdCl.length; l++){
										if($(existProdCl[l]).val() == orgArr[loop].pk_marbasclass){
											break;
										}
									}
									if(l == existProdCl.length){
										childrenDiv.append(div);
									}
								}else{
									childrenDiv.append(div);
								}
							}
							childrenDiv.insertAfter(category_triggerObj.parent());
						}
					});
					
					 category_triggerObj.parent(".prodcate_tree_category").siblings(".prodcate_tree_category").next(".children").slideUp(function(){
						 $(this).prev(".prodcate_tree_category").find(".newProdcate_category_trigger").removeClass("close");
					 });

					return false;
						
				});
		
		$("body").delegate("[name='pkProdcateCheckbox']", 'click', function(){
			//this:��һ��checkbox,IE10��,checkbox��ֵΪon/off;����ʵ�ʵ�val;
			//Ϊ����IE10,��checkboxǰ������һ��input,ֵΪ��ѡ����,����������$(this).prev().val();
			var val = $(this).prev().val();
			if($(this).attr("checked") == "checked"){
				var name = $(this).next("label").text();
				var qaDesc = $(this).next("label").attr("qaDesc");
				$("#newProdPkChecked").append($("<input></input>").attr("id", val).val(val).attr("type","hidden").attr("prodname", name).attr("qaDesc",qaDesc));
			}else{
				$("#"+val).remove();
			}
		});
		
		$(".new_delete_prodCate").live('click', function() {
			var thisObj = $(this);
			var option = {
				title : getLanMsgs("0ec22180-100163"),
				content : getLanMsgs("0ec22180-100188"),
				confirm : function() {
					thisObj.parents(".new_prodCate_div").remove();
					return true;
				}
			}
			showConfirmDialog(option);
			return false;
		});
		
		$(".new_prodCate").live('click',function() {
			if($(this).next(".new_prods").find(".new_prod").length == 0) return ;
			var isHidden = $(this).next().is(":hidden");
			if(isHidden){
				$("#newProdCateDivs").find(".new_prods:visible").slideUp(function(){
					$(this).parent().find("img").
					attr('src',ebvp_context + '/commonres/common/frame/src/img/arrow_down_2.png'); 
				});
				$(this).find("img").attr('src',ebvp_context + '/commonres/common/frame/src/img/arrow_up_2.png');
				$(this).next().slideDown();
			}else{
				$(this).find("img").attr('src',ebvp_context + '/commonres/common/frame/src/img/arrow_down_2.png'); 
				$(this).next().slideUp();
			}
		});
		
		$(".chooseAllLink").live('click',function() {
			var pk_org = $("#vendorOrg").val();
			var pk_group = $("#vendorGroup").val();
			if( (pk_org==null||pk_org=="")  ){
				showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
				return;
			}
			
			var pk_parent = $(this).siblings("[name='prodcate_intree']").val();
			var margin = $(this).siblings(".newProdcate_category_trigger").addClass("close").attr("margin");
			var chooseAllLink = $(this);
			var myts = new Date();
			var formStr = $('#prod_class_filter').serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context
						+ "/register/qryChildrenMarBasClsByPkParent?pk_parent="+pk_parent+"&myts=" + myts+"&pk_org="+pk_org+"&pk_group="+pk_group+"&margin="+margin,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
				var childrenDiv = $("<div></div>").addClass("children").html(result);
				
				var existProdCl = $("#newProdCateDivs").find("[name='pkProdCl']");
				if(existProdCl != null && existProdCl.length !=0){
					for(var l=0; l<existProdCl.length; l++){
						var existPk = $(existProdCl[l]).val();
						childrenDiv.find("[value='"+existPk+"']").parent().remove();
					}
				}
				
				var parent = chooseAllLink.parent();
				parent.next(".children").remove();
				childrenDiv.insertAfter(parent);
				
				childrenDiv.find("input:checked").each(function(){
					var val = $(this).prev().val();
					$("#"+val).remove();
					var name = $(this).next("label").text();
					var qaDesc = $(this).next("label").attr("qaDesc");
					if(qaDesc==null||qaDesc=="null"){
						qaDesc="";
					}
					$("#newProdPkChecked").append($("<input></input>").attr("id", val)
							.val(val).attr("type","hidden").attr("prodname", name)
							.attr("qaDesc",qaDesc));
				});
				}
			});
			chooseAllLink.parent(".prodcate_tree_category").siblings(".prodcate_tree_category").next(".children").slideUp(function(){
				 $(this).prev(".prodcate_tree_category").find(".newProdcate_category_trigger").removeClass("close");
			 });
	
		});
	}
}
$(function() {
	var newProdCate = new NewProdCate();
	newProdCate.init();
});
